import 'dart:io';

import 'package:flutter/material.dart';
import 'package:flutter_syt/generated/l10n.dart';
import 'package:flutter_syt/theme/theme_color.dart';
import 'package:flutter_syt/utils/click_utils.dart';
import 'package:flutter_syt/utils/utils_dlog.dart';
import 'package:url_launcher/url_launcher_string.dart';

import '../global_easy_loading.dart';

class MapNavigatorUtil {
  /// 高德地图导航
  static Future<bool> gotoAMap(
      {required String longitude,
      required String latitude,
      required VoidCallback toInstallCallBack}) {
    var url =
        '${Platform.isAndroid ? 'android' : 'ios'}amap://navi?sourceApplication=amap&lat=$latitude&lon=$longitude&dev=0&style=2';

    return gotoMap(
        url: url,
        toInstallCallBack: () {
          toInstallCallBack();
        });
  }

  /// 腾讯地图导航
  static Future<bool> gotoTencentMap(
      {required String longitude,
      required String latitude,
      required VoidCallback toInstallCallBack}) async {
    var url =
        'qqmap://map/routeplan?type=drive&fromcoord=CurrentLocation&tocoord=$latitude,$longitude&referer=IXHBZ-QIZE4-ZQ6UP-DJYEO-HC2K2-EZBXJ';

    return gotoMap(
        url: url,
        toInstallCallBack: () {
          toInstallCallBack();
        });
  }

  /// 百度地图导航
  static Future<bool> gotoBaiduMap(
      {required String longitude,
      required String latitude,
      required VoidCallback toInstallCallBack}) async {
    var url =
        'baidumap://map/direction?destination=$latitude,$longitude&coord_type=gcj02&mode=driving';
    return gotoMap(
        url: url,
        toInstallCallBack: () {
          toInstallCallBack();
        });
  }

  static Future<bool> gotoAppleMap(
      {required String longitude,
      required String latitude,
      required VoidCallback toInstallCallBack}) async {
    var url = 'http://maps.apple.com/?&daddr=$latitude,$longitude';
    return gotoMap(
        url: url,
        toInstallCallBack: () {
          toInstallCallBack();
        });
  }

  /// 跳转到第三方地图
  /// [url]跳转地址
  /// [toInstallCallBack]地图未安装回调
  static Future<bool> gotoMap(
      {required String url, required VoidCallback toInstallCallBack}) async {
    bool canLaunchUrl = await isMapInstall(url);
    if (!canLaunchUrl) {
      toInstallCallBack();
      return false;
    }
    await launchUrlString(url);
    return true;
  }

  static void toInstallMap(String url) {
    launchUrlString(url);
  }

  static Future<bool> isBaiduMapInstall() {
    return canLaunchUrlString('baidumap://map/direction');
  }

  static Future<bool> isTencentMapInstall() {
    return canLaunchUrlString('qqmap://map/routeplan');
  }

  static Future<bool> isAmapMapInstall() {
    return canLaunchUrlString(
        '${Platform.isAndroid ? 'android' : 'ios'}amap://navi');
  }

  /// 判断地图是否有安装
  static Future<bool> isMapInstall(String url) async {
    return canLaunchUrlString(url);
  }

  static showBottom(BuildContext context,
      {required String longitude, required String latitude}) async {
    var item = [
      S.of(context).map_amap,
      S.of(context).map_baidu,
      S.of(context).map_tencent
    ];
    if (Platform.isIOS) {
      item.add(S.of(context).map_apple);
    }

    DLog('daslkjdlkasjd   ${item.length}');

    return showModalBottomSheet(
      shape: RoundedRectangleBorder(
          borderRadius: BorderRadius.only(
        topLeft: Radius.circular(8),
        topRight: Radius.circular(8),
      )),
      isScrollControlled: true,
      context: context,
      builder: (BuildContext context) {
        return ListView.separated(
          separatorBuilder: (context, index) {
            return Divider(
              height: 1,
              color: ThemeColor.colorBlack40,
            );
          },
          shrinkWrap: true,
          itemCount: item.length,
          itemBuilder: (BuildContext context, int index) {
            return ListTile(
                title: Center(
                  child: Text(
                    item[index],
                    style: TextStyle(
                      fontSize: 14,
                      color: ThemeColor.colorBlack100,
                    ),
                  ),
                ),
                onTap: ClickUtils.debounce(
                  () {
                    if (item[index] == S.of(context).map_apple) {
                      MapNavigatorUtil.gotoAppleMap(
                          longitude: longitude,
                          latitude: latitude,
                          toInstallCallBack: () {
                            customShowToast(S
                                .of(context)
                                .map_mapInstall(S.of(context).map_apple));
                          });
                    } else if (item[index] == S.of(context).map_amap) {
                      MapNavigatorUtil.gotoAMap(
                          longitude: longitude,
                          latitude: latitude,
                          toInstallCallBack: () {
                            customShowToast(S
                                .of(context)
                                .map_mapInstall(S.of(context).map_amap));
                          });
                    } else if (item[index] == S.of(context).map_baidu) {
                      MapNavigatorUtil.gotoBaiduMap(
                          longitude: longitude,
                          latitude: latitude,
                          toInstallCallBack: () {
                            customShowToast(S
                                .of(context)
                                .map_mapInstall(S.of(context).map_baidu));
                          });
                    } else if (item[index] == S.of(context).map_tencent) {
                      MapNavigatorUtil.gotoTencentMap(
                          longitude: longitude,
                          latitude: latitude,
                          toInstallCallBack: () {
                            customShowToast(S
                                .of(context)
                                .map_mapInstall(S.of(context).map_tencent));
                          });
                    }
                  },
                ));
          },
        );
      },
    );
  }
}
